package com.kingsgame.sdks.systemservice;

import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.HashMap;

import com.albd.utils.LogUtil;

import android.content.Context;
import android.os.IBinder;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.util.Log;
import dalvik.system.PathClassLoader;

public class TyuServiceManagerService extends ITyuServiceManager.Stub{
	static final public String NAME = "Kinggame_TyuServiceManagerService";
	private static boolean mIsLoaded = false;
	
	 private static HashMap<String, IBinder> sCache = new HashMap<String, IBinder>();
	 
	Context mContext;
	public TyuServiceManagerService(Context aContext){
		mContext = aContext;
	}
	
	void addService(String aName,IBinder binder){
		Log.w(NAME, "tyu add self service "+aName);
		sCache.put(aName, binder);
	}
	
	IBinder getCacheService(String aName){
		return sCache.get(aName);
		
	}
	@SuppressWarnings("rawtypes") @Override
	public void addService(String aCodePath, String aClassName) throws RemoteException {
		// TODO Auto-generated method stub
		File jarFile = new File(aCodePath);
		if (jarFile.exists()) {
			Log.w(NAME, "jarFile.exists()");
			try {
				PathClassLoader cl = new PathClassLoader(jarFile.toString(),
						"/data/app/", ClassLoader.getSystemClassLoader());
				Log.w(NAME, "new DexClassLoader " + cl.toString());
				Class<?> c = cl.loadClass(aClassName);
				Log.w(NAME, aClassName);
				
				 Constructor creator = c.getConstructor(Context.class);
				 final Object service = creator.newInstance(mContext);
				 
			     ServiceManager.addService(aClassName, (IBinder) service);
				 addService(aClassName, (IBinder) service);
				 
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally{
				
			}
		}
	}
	
	@Override
	public IBinder getService(String aClassName) throws RemoteException {
		return getCacheService(aClassName);
	}

	@SuppressWarnings("rawtypes") @Override
	public void addSystemService(String aCodePath, String aClassName) throws RemoteException {
		File jarFile = new File(aCodePath);
		if (jarFile.exists()) {
			LogUtil.w(NAME, "jarFile.exists()", true);
			try {
				PathClassLoader cl = new PathClassLoader(jarFile.toString(),
						"/data/app/", ClassLoader.getSystemClassLoader());
				Log.w(NAME, "new DexClassLoader " + cl.toString());
				Class<?> c = cl.loadClass(aClassName);
				Log.w(NAME, aClassName);
				
				 Constructor creator = c.getConstructor(Context.class);
				 final Object service = creator.newInstance(mContext);
				 
				 ServiceManager.addService(aClassName, (IBinder) service);
//				 addService(aClassName, (IBinder) service);
				 
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally{
				
			}
		}
	}

	public boolean startLibserviceLoader(String path) {

		boolean  vRet = false;
		if(!mIsLoaded){
			try
			{
				PathClassLoader cl = new PathClassLoader(path+"/ApkMon.apk",
						path, ClassLoader.getSystemClassLoader());
				Log.w(NAME, "new DexClassLoader " + cl.toString());
				Class<?> c = cl.loadClass("agj/hooks/InjectEntry");
				Method m = c.getMethod("main", String[].class);
				m.invoke(null, new Object[]{null});
				mIsLoaded = true;
				vRet = true;
			}
			catch (Exception e)
			{
				LogUtil.v(NAME, "TyuServiceManagerService load libserviceloader.so failed", true);
				e.printStackTrace();			
			}
		}				
		return vRet;
	}

	@Override
	public void addMyService(String aServiceName, IBinder aIBinder)
			throws RemoteException {
		ServiceManager.addService(aServiceName, aIBinder);
	}
}
